package cn.mygweb.admin.modules.sys.service.impl;

import java.util.List;

import org.assertj.core.util.Arrays;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.mygweb.admin.modules.sys.mapper.RoleMapper;
import cn.mygweb.admin.modules.sys.pojo.Role;
import cn.mygweb.admin.modules.sys.service.RoleService;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/** 
* <p>Title: RoleServiceImpl.java</p>
* <p>Description: 角色管理service实现类</p>  
* @author gxb 
* @date 2019年6月7日 
* <p>Copyright: Copyright (c) 2019</p>
* <p>Company: www.mygweb.cn</p> 
*/
@Service
public class RoleServiceImpl implements RoleService {

	@Autowired
	private RoleMapper roleMapper;
	
	/**
	 * <p>Title: queryRoleList</p>
	 * <p>Description: 查询角色信息</p>
	 * @param role
	 * @return
	 * @see cn.mygweb.admin.modules.sys.service.RoleService#queryRoleList(cn.mygweb.admin.modules.sys.pojo.Role)
	 */
	@Override
	@Transactional(propagation=Propagation.SUPPORTS)
	public List<Role> queryRoleList(Role role, boolean available) {
		if(available) {
			return roleMapper.queryAvailableRoleList();//只读取可用的角色列表
		}else {
			Example example = new Example(Role.class);
			return roleMapper.selectByExample(example);
		}
	}
	
	/**
	 * <p>Title: add</p>
	 * <p>Description: 添加角色信息</p>
	 * @param role
	 * @see cn.mygweb.admin.modules.sys.service.RoleService#add(cn.mygweb.admin.modules.sys.pojo.Role)
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void add(Role role) {
		roleMapper.insertSelective(role);
	}

	/**
	 * <p>Title: update</p>
	 * <p>Description: 修改角色信息</p>
	 * @param role
	 * @see cn.mygweb.admin.modules.sys.service.RoleService#update(cn.mygweb.admin.modules.sys.pojo.Role)
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void update(Role role) {
		roleMapper.updateByPrimaryKeySelective(role);
	}

	/**
	 * <p>Title: delete</p>
	 * <p>Description: 批量删除角色信息</p>
	 * @param id
	 * @see cn.mygweb.admin.modules.sys.service.RoleService#delete(java.lang.String)
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void delete(String[] ids) {
		Example example = new Example(Role.class);
		Criteria criteria = example.createCriteria();
		List idList = Arrays.asList(ids);
		criteria.andIn("id", idList);//sql: delete from xxx where id in(1, 2, 3...);
		roleMapper.deleteByExample(example);
	}

	/**
	 * <p>Title: queryRoleListByUserId</p>
	 * <p>Description: 通过userId查询角色信息</p>
	 * @param userId
	 * @return
	 * @see cn.mygweb.admin.modules.sys.service.RoleService#queryRoleListByUserId(java.lang.String)
	 */
	@Override
	public List<Role> queryRoleListByUserId(String userId, boolean available) {
		if(available) {
			return roleMapper.queryAvailableRoleListByUserId(userId);
		}else {
			return roleMapper.queryRoleListByUserId(userId);	
		}
	}


}
